package com.xrtn.controller;

import com.xrtn.bean.SysUserInfo;
import com.xrtn.service.ICommonService;
import com.xrtn.service.ISysAuthService;
import com.xrtn.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tu_pei on 2018/2/26.
 */

@Api("验证接口")
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
public class AuthController {

    @Autowired
    private ISysAuthService authService;

    @Autowired
    private ICommonService commonService;

    @RequestMapping(value = "/gotoLogin")
    public String login() {
        return "login";
    }
    @RequestMapping(value = "/index")
    public ModelAndView index(ModelAndView model) {
        model.setViewName("home/home");
        return model;
    }
    /**
     * @param @param  account
     * @param @param  userPwd
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: sign
     * @Description: 后台登录
     */
    @ApiOperation(value = "登录", notes = "后台登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userNo", dataType = "string", required = true, value = "员工编号", defaultValue = "GYT000001"),
            @ApiImplicitParam(paramType = "query", name = "userPwd", dataType = "string", required = true, value = "密码", defaultValue = "123456")
    })
    @RequestMapping(value = "/sign", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData sign(HttpServletRequest request, String userNo, String userPwd, ModelAndView model) {
        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        Map<String, Object> resultMap = authService.sysSign(userNo, userPwd);
        // 获取返回信息
        CodeEnum resultMsg = (CodeEnum) resultMap.get("EnumCode");
        // 如果不为空
        if (ObjUtils.isNotEmpty(resultMsg)) {
            // 设置返回数据
            data.setCodeEnum(resultMsg);

            model.setViewName("login");
            return data;
            //return model;
        }
        // 续费过期验证
//        Date now = new Date();
//        Date passDate = null;
//        try {
//            passDate = DateUtils.parse("2019-01-14 00:00:00");
//        } catch (Exception e) {
//
//        }
//        if (passDate != null && passDate.getTime() - now.getTime() > 0) {
//            data.setCodeEnum(CodeEnum.CODE_10021);
//        } else {
//            data.setCodeEnum(CodeEnum.CODE_10022);
//            return data;
//        }
        // 设置返回对象
        HttpUtils.setUserPojo2Session(request, resultMap.get("userInfo"));
        System.out.println("userInfo = " + JSONObject.fromObject(resultMap.get("userInfo")));
        // 返回数据
        //model.addObject("userInfo", resultMap.get("userInfo"));
        //model.setViewName("home/home");
        data.setResults(resultMap.get("userInfo"));
        return data;
    }

    @ApiOperation(value = "修改密码", notes = "改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "员工ID", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "oldPwd", dataType = "string", required = true, value = "旧密码", defaultValue = "123456"),
            @ApiImplicitParam(paramType = "query", name = "userPwd", dataType = "string", required = true, value = "密码", defaultValue = "654321")
    })
    @RequestMapping(value = "/changepwd",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData changePwd(int userId, String oldPwd, String userPwd) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userId, userPwd});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        } else {
            int count = authService.updatePwd(userId, oldPwd, userPwd);
            if (count < 0) {
                data.setCodeEnum(CodeEnum.CODE_90002);
            } else {
                data.setResults(count);
            }
            // 返回数据
            return data;
        }
    }
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "员工ID", defaultValue = "1"),
    })
    @RequestMapping(value = "/resetUserPwd",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData resetUserPwd(int userId) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userId});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }
        return authService.updateUserPwd(userId, data);
    }

    @ApiOperation(value = "获取首页统计数据", notes = "获取首页统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "分店id", defaultValue = "1")
    })
    @RequestMapping(value = "/selectDesktopInfo",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectDesktopInfo(HttpServletRequest request,Integer subclinicId) {

        SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);
        ResponseData data = new ResponseData();

        Map<String,Object> params = HttpUtils.getAllReqParams(request);

        //ObjUtils.putSubclinicId(userInfo,params,subclinicId);

        data.setResults(commonService.selectSysDesktop(params));

        return data;
    }
}